//
//  YHAutoScrollCanvas.cpp
//  ThunderWarriors
//
//  Created by mac on 13-6-20.
//
//

#include "YHAutoScrollCanvas.h"
USING_NS_CC;

void YHAutoScrollCanvasDelegate::WillRenderCanvas(){
    
}

void YHAutoScrollCanvasDelegate::DidEndRenderCanvas(){
    
}

IMPLEMENT_CREATE_CLASS(YHAutoScrollCanvas)



YHAutoScrollCanvas::YHAutoScrollCanvas()
{
    m_isAutoScroll = false;
    m_canvas2 = NULL;
    m_canvas1 = NULL;
    m_canvas0 = NULL;
    m_AutoScrollCanvasDelegate = NULL;
}

YHAutoScrollCanvas::~YHAutoScrollCanvas()
{
    
}

void YHAutoScrollCanvas::setDelegate(YHAutoScrollCanvasDelegate* delegate)
{
    m_AutoScrollCanvasDelegate = delegate;
}

YHAutoScrollCanvas* YHAutoScrollCanvas::createWithSize(CCSize size)
{
    YHAutoScrollCanvas* pRet = new YHAutoScrollCanvas();
    if (pRet && pRet->initWithSize(size)) {
        pRet->autorelease();
        return pRet;
    }
    delete pRet;
    return NULL;
}

bool YHAutoScrollCanvas::initWithSize(CCSize size)
{
    m_canvasSize = size;
    
    m_canvas0 = CCRenderTexture::create(size.width, size.height);
    m_canvas0->setTag(KTagCanvas0);
    m_canvas1 = CCRenderTexture::create(size.width, size.height);
    m_canvas1->setTag(KTagCanvas1);
    m_canvas2 = CCRenderTexture::create(size.width, size.height);
    m_canvas2->setTag(KTagCanvas2);
    
    m_canvas0->setPosition(CCPointMake(m_canvasSize.width/2, m_canvasSize.height/2));
    m_canvas1->setPosition(CCPointMake(m_canvasSize.width/2, m_canvasSize.height/2*3));
    m_canvas2->setPosition(CCPointMake(m_canvasSize.width/2, m_canvasSize.height/2*5));
    
    this->addChild(m_canvas0);
    this->addChild(m_canvas1);
    this->addChild(m_canvas2);
    
    return true;
}


/*开始滚动，speed表示速度*/
void YHAutoScrollCanvas::beginAutoScroll(float speed)
{
    renderCanvas(m_canvas0);
    renderCanvas(m_canvas1);
    renderCanvas(m_canvas2);
    
    float t = m_canvasSize.height / speed;
    CCLOG("t=%f",t);
    /*Canvas0*/
    CCMoveBy* moveBy_Canvas0 = CCMoveBy::create(t, CCPointMake(0, - m_canvasSize.height));
    CCMoveTo* moveTo_Canvas0 = CCMoveTo::create(0, CCPointMake(m_canvasSize.width/2, m_canvasSize.height/2*5));
    CCCallFuncN* callFunc_Canvas0 = CCCallFuncN::create(this, callfuncN_selector(YHAutoScrollCanvas::renderCanvas));
    CCMoveBy* moveBy2_Canvas0 = CCMoveBy::create(2*t, CCPointMake(0, -2 * m_canvasSize.height));
    CCFiniteTimeAction* sequence_canvas0 = CCSequence::create(moveBy_Canvas0, moveTo_Canvas0, callFunc_Canvas0,
                                                    moveBy2_Canvas0, NULL);
    CCRepeatForever* repeat_Canvas0 = CCRepeatForever::create((CCActionInterval*)sequence_canvas0);
    m_canvas0->runAction(repeat_Canvas0);
    
    /*Canvas1*/
    CCMoveBy* moveBy_Canvas1 = CCMoveBy::create(2*t, CCPointMake(0, - 2*m_canvasSize.height));
    CCMoveTo* moveTo_Canvas1 = CCMoveTo::create(0, CCPointMake(m_canvasSize.width/2, m_canvasSize.height/2*5));
    CCCallFuncN* callFunc_Canvas1 = CCCallFuncN::create(this, callfuncN_selector(YHAutoScrollCanvas::renderCanvas));
    CCMoveBy* moveBy2_Canvas1 = CCMoveBy::create(t, CCPointMake(0, - m_canvasSize.height));
    CCFiniteTimeAction* sequence_canvas1 = CCSequence::create(moveBy_Canvas1, moveTo_Canvas1, callFunc_Canvas1,
                                                      moveBy2_Canvas1, NULL);
    CCRepeatForever* repeat_Canvas1 = CCRepeatForever::create((CCActionInterval*)sequence_canvas1);
    m_canvas1->runAction(repeat_Canvas1);
    
    /*Canvas2*/
    CCMoveBy* moveBy_Canvas2 = CCMoveBy::create(3*t, CCPointMake(0, - 3 * m_canvasSize.height));
    CCMoveTo* moveTo_Canvas2 = CCMoveTo::create(0, CCPointMake(m_canvasSize.width/2, m_canvasSize.height/2*5));
    CCCallFuncN* callFunc_Canvas2 = CCCallFuncN::create(this, callfuncN_selector(YHAutoScrollCanvas::renderCanvas));
    CCFiniteTimeAction* sequence_canvas2 = CCSequence::create(moveBy_Canvas2, moveTo_Canvas2, callFunc_Canvas2,NULL);
    CCRepeatForever* repeat_Canvas2 = CCRepeatForever::create((CCActionInterval*)sequence_canvas2);
    m_canvas2->runAction(repeat_Canvas2);
    
}

void YHAutoScrollCanvas::stopAutoScroll()
{
    m_canvas0->stopAllActions();
    m_canvas1->stopAllActions();
    m_canvas2->stopAllActions();
}

//tag表示下一个要渲染的canvas
void YHAutoScrollCanvas::renderCanvas(CCNode* canvas)
{
    CCRenderTexture* renderCanvas = (CCRenderTexture*) canvas;
    if (renderCanvas) {
        switch (renderCanvas->getTag()) {
            case KTagCanvas0:
            {
                if (m_AutoScrollCanvasDelegate) {
                    m_AutoScrollCanvasDelegate->WillRenderCanvas();
                    m_canvas0->beginWithClear(1,0,0,1);
                    m_AutoScrollCanvasDelegate->RenderCanvas(m_canvas0->getPosition());
                    m_canvas0->end();
                    m_AutoScrollCanvasDelegate->DidEndRenderCanvas();
                }
            }
                break;
            case KTagCanvas1:
            {
                if (m_AutoScrollCanvasDelegate) {
                    m_AutoScrollCanvasDelegate->WillRenderCanvas();
                    m_canvas1->beginWithClear(0,1,0,1);
                    m_AutoScrollCanvasDelegate->RenderCanvas(m_canvas1->getPosition());
                    m_canvas1->end();
                    m_AutoScrollCanvasDelegate->DidEndRenderCanvas();
                }
            }
                break;
            case KTagCanvas2:
            {
                if (m_AutoScrollCanvasDelegate) {
                    m_AutoScrollCanvasDelegate->WillRenderCanvas();
                    m_canvas2->beginWithClear(0,0,1,1);
                    m_AutoScrollCanvasDelegate->RenderCanvas(m_canvas2->getPosition());
                    m_canvas2->end();
                    m_AutoScrollCanvasDelegate->DidEndRenderCanvas();
                }
            }
                break;
            default:
                break;
        }
    }
}



